/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlutils;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.xerces.dom.ParentNode;
import org.w3c.dom.*;

/**
 *
 * @author TungNT60513
 */
public class PlayerUtils {

    public void likeControl(String songID, String filePath) {
        try {
            Document doc = xmlutils.XMLUtils.parseDOM(filePath);
            XPathFactory xpf = XPathFactory.newInstance();
            XPath xp = xpf.newXPath();

            String exp = "//*[local-name()='Song']/*[local-name()='SongID' and text()= '" + songID + "']/parent::*/*[local-name()='LikeNumber']"; // tim songID, lay likenumber
            Node likeNode = (Node) xp.evaluate(exp, doc, XPathConstants.NODE); // tra ve node likenumber

            int number = Integer.parseInt(likeNode.getTextContent());
            int likeNumber = number + 1;
            likeNode.setTextContent(Integer.toString(likeNumber));
            xmlutils.XMLUtils.writeXML(doc, filePath); // goi ham writeXML ghi xuong file xml
        } catch (XPathExpressionException ex) {
            Logger.getLogger(PlayerUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
    } // end likeControl

    public void unlikeControl(String songID, String filePath) {
        try {
            Document doc = xmlutils.XMLUtils.parseDOM(filePath);
            XPathFactory xpf = XPathFactory.newInstance();
            XPath xp = xpf.newXPath();

            String exp = "//*[local-name()='Song']/*[local-name()='SongID' and text()= '" + songID + "']/parent::*/*[local-name()='LikeNumber']";
            //String exp = "//*[local-name()='Songs']//*[local-name()='SongID' and text()='" + songID + "']/LikeNumber"; // tim songID, lay likenumber
            Node likeNode = (Node) xp.evaluate(exp, doc, XPathConstants.NODE); // tra ve node likenumber
            
            int number = Integer.parseInt(likeNode.getTextContent()); // chuyen unlikenumber thanh kieu int
            int unlikeNumber = number - 1;
            likeNode.setTextContent(Integer.toString(unlikeNumber));
            xmlutils.XMLUtils.writeXML(doc, filePath); // goi ham writeXML
        } catch (XPathExpressionException ex) {
            Logger.getLogger(PlayerUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
    } // end unlikeControl

    public void subscribeControl(String artistID, String filePath) {
        try {
            Document doc = xmlutils.XMLUtils.parseDOM(filePath);
            XPathFactory xpf = XPathFactory.newInstance();
            XPath xp = xpf.newXPath();
            
            String exp = "//Artist[@ArtistID='" + artistID + "']/SubscriberNumber"; // tim artistID, lay subscribe number
            Node subsNode = (Node) xp.evaluate(exp, doc, XPathConstants.NODE); // tra ve node subscriberNumber

            int number = Integer.parseInt(subsNode.getTextContent()); // chuyen subsnumber thanh kieu int
            int subNumber = number + 1;
            subsNode.setTextContent(Integer.toString(subNumber)); // lay so subsnumber hien co + 1
            xmlutils.XMLUtils.writeXML(doc, filePath); // goi ham writeXML
        } catch (XPathExpressionException ex) {
            Logger.getLogger(PlayerUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
    } // end subscribeControl

    public void unsubscribeControl(String artistID, String filePath) {
        try {
            Document doc = xmlutils.XMLUtils.parseDOM(filePath);
            XPathFactory xpf = XPathFactory.newInstance();
            XPath xp = xpf.newXPath();
            
            String exp = "//Artist[@ArtistID='" + artistID + "']/SubscriberNumber"; // lay subscribe number
            Node subsNode = (Node) xp.evaluate(exp, doc, XPathConstants.NODE); // tra ve node subscriberNumber

            int number = Integer.parseInt(subsNode.getTextContent()); // chuyen subsnumber thanh kieu int
            int unsubsNumber = number - 1;
            subsNode.setTextContent(Integer.toString(unsubsNumber)); // lay so subsnumber hien co - 1
            xmlutils.XMLUtils.writeXML(doc, filePath);

        } catch (XPathExpressionException ex) {
            Logger.getLogger(PlayerUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
    } // end UnsubscribeControl
} // end PlayerUtils

